/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.search;

import br.com.deployment.daoImpl.CityDAOImpl;
import br.com.deployment.daoImpl.OrganDAOImpl;
import br.com.deployment.entities.City;
import br.com.deployment.entities.Organ;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author luciano
 */
@ManagedBean
@SessionScoped
public class ManagerSearchOrgan {
    
   @EJB
   OrganDAOImpl organService;
   @EJB
   CityDAOImpl cityService;
   //Utilizado no show;
   private Organ organ;
   
   
   private List<Organ> organs;
   
   private Organ searchOrgan;
   private City city;

    public ManagerSearchOrgan() {
        this.city = new City();
        this.searchOrgan = new Organ();
    }
   
    public List<Organ> search(){
        if(this.city != null && this.city.getId() != null)
            this.city = cityService.find(this.city.getId());
        this.searchOrgan.setCity(city);
        return organService.find(searchOrgan);
    }
    
   public List<Organ> lstOrgan(){
       return organService.findAll();
   }

    public Organ getOrgan() {
        return organ;
    }

    public void setOrgan(Organ organ) {
        this.organ = organ;
    }

   
    
      // Mostra o Objeto Executor Selecionado e Redireciona para a página
    public void redirect(String string, Organ o) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            this.organ = o;
            this.organ.setCity(cityService.find(this.organ.getCity().getId()));
            System.out.println(o);
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerEditExecutor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }


    public List<Organ> getOrgans() {
        return organs;
    }

    public void setOrgans(List<Organ> organs) {
        this.organs = organs;
    }

    public Organ getSearchOrgan() {
        return searchOrgan;
    }

    public void setSearchOrgan(Organ searchOrgan) {
        this.searchOrgan = searchOrgan;
    }
    
}
